package com.tanhua.dubbo.api.impl.mongo;

import com.tanhua.domain.mongo.Comment;
import com.tanhua.domain.mongo.Publish;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.dubbo.api.mongo.CommentApi;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@Service
public class CommentApiImpl implements CommentApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    //保存评论
    @Override
    public int save(Comment comment) {
        //1. 保存评论
        mongoTemplate.save(comment);

        //2. 对朋友圈的(点赞|评论|喜欢)数量进行+1
        Query query = new Query(Criteria.where("_id").is(comment.getPublishId()));
        Update update = new Update();
        update.inc(comment.getCol(),1);
        mongoTemplate.updateFirst(query,update, Publish.class);

        //3. 查询当前动态的评论数、点赞数、喜欢数
        Publish publish = mongoTemplate.findById(comment.getPublishId(), Publish.class);

        if(comment.getCommentType()==1){
            //点赞
            return publish.getLikeCount();
        }else if(comment.getCommentType()==2){
            //评论
            return publish.getCommentCount();
        }else{
            return publish.getLoveCount();
        }
    }

    @Override
    public int delete(Comment comment) {
        //1. 删除评论
        Query commentQuery = new Query(Criteria.where("publishId").is(comment.getPublishId())
        .and("commentType").is(comment.getCommentType())
        .and("pubType").is(comment.getPubType())
        .and("userId").is(comment.getUserId()));
        mongoTemplate.remove(commentQuery,Comment.class);

        //2. 对朋友圈的(点赞|评论|喜欢)数量进行-1
        Query query = new Query(Criteria.where("_id").is(comment.getPublishId()));
        Update update = new Update();
        update.inc(comment.getCol(),-1);
        mongoTemplate.updateFirst(query,update, Publish.class);

        //3. 查询当前动态的评论数、点赞数、喜欢数
        Publish publish = mongoTemplate.findById(comment.getPublishId(), Publish.class);

        if(comment.getCommentType()==1){
            //点赞
            return publish.getLikeCount();
        }else if(comment.getCommentType()==2){
            //评论
            return publish.getCommentCount();
        }else{
            return publish.getLoveCount();
        }
    }

    @Override
    public PageResult queryCommentsList(String movementId, Integer page, Integer pagesize) {
        Query query = new Query(Criteria.where("publishId").is(new ObjectId(movementId))
        .and("commentType").is(2)  //1 点赞， 2：评论
        .and("pubType").is(1));  //产品类型：1 朋友圈
        query.with(Sort.by(Sort.Order.desc("created"))).skip((page-1)*pagesize).limit(pagesize);

        List<Comment> list = mongoTemplate.find(query, Comment.class);
        long counts = mongoTemplate.count(query, Comment.class);


        PageResult pageResult = new PageResult(page,pagesize,(int)counts,list);
        return pageResult;
    }
}
